package com.itaming.lycheeframework.support.cipher.symmetric;

import com.itaming.lycheeframework.support.cipher.CipherConstant;
import org.springframework.util.Assert;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * SM4加密器
 *
 * @author A.Ming
 */
public class Sm4Encryptor extends AbstractSymmetricEncryptor {

    /**
     * 获取初始化向量长度，单位：bit
     *
     * @return 初始化向量长度
     */
    @Override
    protected int getIvBitLength() {
        return CipherConstant.SM4_IV_BIT_LENGTH;
    }

    /**
     * 获取SecretKey
     *
     * @param key          密钥
     * @param keyBitLength 密钥长度(bit)
     * @return SecretKey
     */
    @Override
    protected SecretKey getSecretKey(String key, int keyBitLength) {
        Assert.isTrue(keyBitLength == 128, "SM4 secret key must be 128 bit");
        return new SecretKeySpec(getKeyBytes(key, keyBitLength), CipherConstant.ALGORITHM_SM4);
    }

}
